package persistencia;

import java.io.File;

import modelo.tp5.FileUtils;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class DbGrafos {

    private static DbGrafos graph = null;
    private static final String DB_PATH = "/home/guidop/Desktop/baseNeo4j/";
    private static GraphDatabaseService graphService = null;

    public DbGrafos() {
        try{
    	FileUtils.deleteRecursively( new File( DB_PATH ) );
        graphService = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        registerShutdownHook(graphService);
        }catch(Exception e){
            File file = new File(DB_PATH+"/tm_tx_log.1");
            file.delete();
        }
    }

    public static DbGrafos getInstance() {
        if (graph == null) {
            graph = new DbGrafos();
        }
        return graph;
    }

    public GraphDatabaseService getGraphDbService() {
        return graphService;

    }

    

    public void shutDown() {
        System.out.println();
        System.out.println("Shutting down database ...");
        graphService.shutdown();
    }

    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                graphDb.shutdown();
            }
        });
    }    

}
